package com.icen.utils.comm;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.nfc.NfcAdapter;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;

/**
 * Mobile Phone Hardware Information Utils Class
 * 
 * @author IcenHan
 * 
 */
public class HardInfoUtils {
	/**
	 * 获取手机IMEI号
	 * 
	 * @param context
	 *            上下文
	 * @return String
	 */
	public static String getImei(Context context) {
		TelephonyManager telphonyManager = (TelephonyManager) context
				.getSystemService(Context.TELEPHONY_SERVICE);
		return telphonyManager.getDeviceId();
	}

	/**
	 * 获取手机SIM卡序列号
	 * 
	 * @param context
	 *            上下文
	 * @return String
	 */
	public static String getSimSerialNumber(Context context) {
		TelephonyManager telphonyManager = (TelephonyManager) context
				.getSystemService(Context.TELEPHONY_SERVICE);
		return telphonyManager.getSimSerialNumber();
	}

	private static final int WIDTH = 0x123;
	private static final int HEIGHT = 0x234;

	/**
	 * 获取手机屏幕分辨率
	 * 
	 * @param activity
	 *            Activity
	 * @return String
	 *         <p>
	 *         格式:720x1280
	 *         </p>
	 */
	private static int getScreenSize(Activity activity, int type) {
		DisplayMetrics displayMetrics = new DisplayMetrics();
		activity.getWindowManager().getDefaultDisplay()
				.getMetrics(displayMetrics);

		int width = displayMetrics.widthPixels;
		int height = displayMetrics.heightPixels;
		if (type == HardInfoUtils.WIDTH) {
			return width;
		} else if (type == HardInfoUtils.HEIGHT) {
			return height;
		}

		return -1;
	}

	/**
	 * 获取手机屏幕宽度
	 * 
	 * @param activity
	 * @return int
	 *         <p>
	 *         return -1 : 表示获取失败
	 *         </p>
	 */
	public static int getScreenSize_Width(Activity activity) {
		return HardInfoUtils.getScreenSize(activity, HardInfoUtils.WIDTH);
	}

	/**
	 * 获取手机屏幕高度
	 * 
	 * @param activity
	 * @return int
	 *         <p>
	 *         return -1 : 表示获取失败
	 *         </p>
	 */
	public static int getScreenSize_Height(Activity activity) {
		return HardInfoUtils.getScreenSize(activity, HardInfoUtils.HEIGHT);
	}

	/**
	 * 获取手机屏幕大小: 宽 x 高
	 * 
	 * @param activity
	 * @return String
	 */
	public static String getScreenSize(Activity activity) {
		int width = HardInfoUtils.getScreenSize_Width(activity);
		int height = HardInfoUtils.getScreenSize_Height(activity);
		return width != -1 && height != -1 ? width + "x" + height : "";
	}

	/**
	 * 检查手机网络是否已打开
	 * 
	 * @param context
	 *            上下文
	 * @return boolean
	 *         <p>
	 *         true:网络可用 false:网络不可用
	 *         </p>
	 */
	public static boolean isAliveNetWork(Context context) {

		ConnectivityManager connectivityManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();

		if (networkInfo != null && networkInfo.isConnected()) {
			return networkInfo.isAvailable();
		} else {
			return false;
		}
	}

	/**
	 * 检查手机GPS是否已打开
	 * 
	 * @param context
	 *            上下文
	 * @return boolean
	 *         <p>
	 *         true:已打开 false:未打开
	 *         </p>
	 */
	public static boolean isAliveGPS(Context context) {
		LocationManager locationManager = (LocationManager) context
				.getSystemService(Context.LOCATION_SERVICE);
		return locationManager == null ? false : locationManager
				.isProviderEnabled(LocationManager.GPS_PROVIDER);
	}

	/**
	 * 检查手机是否支持NFC设备
	 * 
	 * @param context
	 *            上下文
	 * @return boolean
	 *         <p>
	 *         true:支持 false:不支持
	 *         </p>
	 */
	public static boolean isHasNFC(Context context) {
		return NfcAdapter.getDefaultAdapter(context) == null ? false : true;
	}

	/**
	 * 检查手机NFC设备是否打开
	 * 
	 * @param context
	 *            上下文
	 * @return boolean
	 *         <p>
	 *         true:打开 false:未打开
	 *         </p>
	 */
	public static boolean isAliveNFC(Context context) {
		NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(context);
		return nfcAdapter == null ? false : nfcAdapter.isEnabled();
	}

	/**
	 * 检查设备是否有闪光灯
	 * 
	 * @param context
	 *            上下文
	 * @return boolean
	 */
	public static boolean isHasFlashlight(Context context) {
		return context.getPackageManager().hasSystemFeature(
				PackageManager.FEATURE_CAMERA_FLASH);
	}

}
